package sem;

import com.ibm.tspaces.TupleSpace;
import com.ibm.tspaces.TupleSpaceException;

public class Test {
  public static void main(String[] args) 
  throws TupleSpaceException, InterruptedException {
    int        rank = Integer.parseInt(args[0]);
    TupleSpace ts   = new TupleSpace("semSpace", "localhost");
    Semaphore  sem  = new Semaphore("sem", ts);
    
    if (rank == 0) {
      int n = Integer.parseInt(args[1]);
      ts.deleteAll();
      sem.init(n);
    }
    
    Thread.sleep(2000);
    
    sem.P();
    for (int i = 0; i < 5; i++)
      System.out.printf("Client %d: print %d\n", rank, i);
    sem.V();
    
    System.err.printf("Client %d finished.\n", rank);
  }
}
